from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
service = Service()
class Crawl_ZhuanLi(object):
    def __init__(self):
        chromeoption = webdriver.ChromeOptions()
        # chromeoption.add_argument('--headless')    # 无头浏览器
        chromeoption.add_argument('--no-sandbox')  # 解决linux DevToolsActivePort文件不存在的报错
        chromeoption.add_argument(
            'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36')
        self.driver = webdriver.Chrome(service=service, chrome_options=chromeoption)

    def get_value(self,url):
        # 下面这行代码目的为:防止网站识别出是selenium
        self.driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
            "source": """
            Object.defineProperty(navigator, 'webdriver', {
              get: () => undefined
            })
          """
        })
        self.driver.get(url)
        print("开始爬取...")
        time.sleep(60)

Crawl_ZhuanLi().get_value(url='http://cpquery.cnipa.gov.cn/')
